//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Globalization;
using System.Web;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Summary description for GallerySlideshowHandler.
	/// </summary>
	public class GallerySlideshowHandler : BaseGallerySyndicationHandler
	{
		public GallerySlideshowHandler()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		protected int CategoryID  = -1;
		
		private int mainImageWidth = -1;
		public int MainImageWidth
		{
			get { return mainImageWidth; }
			set { mainImageWidth = value; }
		}

		private int mainImageHeight = -1;
		public int MainImageHeight
		{
			get { return mainImageHeight; }
			set { mainImageHeight = value; }
		}

		protected override string CacheKey
		{
			get
			{
				if(CurrentSection == null)
					return string.Format("Gallery:{0}:Type:{1}:W:{2}:H{3}", "", "SlideShowPro",this.MainImageWidth, this.MainImageHeight);
				if(CategoryID == -1)
					return string.Format("Gallery:{0}:Type:{1}:W:{2}:H{3}", CurrentSection.SectionID, "SlideShowPro",this.MainImageWidth, this.MainImageHeight);
				else
					return string.Format("Gallery:{0}:Category:{1}:Type:{2}:W:{3}:H{4}", CurrentSection.SectionID, CategoryID, "SlideShowPro",this.MainImageWidth, this.MainImageHeight);
			}
		}

		/// <summary>
		/// Override Process feed to make sure  we check for a CategoryID Querystring
		/// </summary>
		protected override void ProcessFeed()
		{
			if(Context.Request.QueryString["CategoryID"] != null)
				this.CategoryID = Globals.SafeInt(Context.Request.QueryString["CategoryID"], -1);

			if(Context.Request.QueryString["Width"] != null)
				MainImageWidth = Globals.SafeInt(Context.Request.QueryString["Width"], -1);
			
			if(Context.Request.QueryString["Height"] != null)
				MainImageHeight = Globals.SafeInt(Context.Request.QueryString["Height"], -1);

			HttpContext context = CSContext.Current.Context;
			if(context != null)
			{
				context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
				context.Response.Cache.SetLastModified(DateTime.Now);
				context.Response.Cache.SetAllowResponseInBrowserHistory(false);
				context.Response.Cache.SetValidUntilExpires(false);
				context.Response.Cache.SetNoStore();
			}

			base.ProcessFeed ();
		}


		protected override CachedFeed BuildFeed()
		{
			BaseSyndicationWriter writer = null;
			DateTime date = DateTime.Now;

			writer = new GallerySlideshowWriter(CurrentGallery, this.BaseUrl, this.CategoryID, this.MainImageWidth, this.MainImageHeight);

			return new CachedFeed(date, date.ToString() , writer.GetXml());

		}



	}
}
